import axios from 'axios'

// 'http://localhost:8888/todos'

//获取
export const getList = () => {
  return async (dispatch) => {
    const { data: res } = await axios.get('http://localhost:8888/todos')
    // console.log(res)
    dispatch({
      type: 'GET_LIST',
      payload: res,
    })
  }
}
//添加
export const addList = (name) => {
  return async (dispatch) => {
    const { data: res } = await axios.post('http://localhost:8888/todos', {
      name,
      done: false,
    })
    // console.log(res)
    dispatch({
      type: 'ADD_LIST',
      payload: res,
    })
  }
}
//修改状态
export const changeDone = (id, done) => {
  return async (dispatch) => {
    const { data: res } = await axios.patch(
      'http://localhost:8888/todos/' + id,
      {
        done,
      }
    )
    // console.log(res)
    dispatch({
      type: 'CHANGE_DONE',
      payload: {
        id,
        done,
      },
    })
  }
}
//删除
export const delList = (id) => {
  return async (dispatch) => {
    const { data: res } = await axios.delete(
      'http://localhost:8888/todos/' + id
    )
    console.log(res)
    dispatch({
      type: 'DEL_LIST',
      payload: {
        id,
      },
    })
  }
}
//修改内容
export const reviseList = (id, name) => {
  return async (dispatch) => {
    const { data: res } = await axios.patch(
      'http://localhost:8888/todos/' + id,
      {
        name,
      }
    )
    console.log(res)
    dispatch({
      type: 'REVISE_LIST',
      payload: {
        id,
        name,
      },
    })
  }
}
//筛选功能
export const filterList = (todoType) => {
  return {
    type: 'FILTER_LIST',
    payload: {
      todoType,
    },
  }
}
